 package com.ruoyi.web.controller.project.firstclass;


import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.project.firstclass.FirstClass;
import com.ruoyi.system.domain.project.firstclass.FirstClassMember;
import com.ruoyi.system.service.project.IFirstClassMemberService;
import com.ruoyi.system.service.project.IFirstClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 一流学科Controller
 *
 * @author ruoyi
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/project/class")
public class FirstClassController extends BaseController
{
    @Autowired
    private IFirstClassService firstClassService;
    @Autowired
    private IFirstClassMemberService firstClassMemberService;
    /**
     * 查询一流学科列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FirstClass firstClass)
    {
        startPage();
        List<FirstClass> list = firstClassService.selectFirstClassList(firstClass);
        return getDataTable(list);
    }

    /**
     * 导出一流学科列表
     */
    @Log(title = "一流学科", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FirstClass firstClass,
                       @RequestParam(required = false) String exportFields)
    {
        List<FirstClass> list = firstClassService.selectFirstClassList(firstClass);
        ExcelUtil<FirstClass> util = new ExcelUtil<FirstClass>(FirstClass.class);

        // 如果传递了导出字段，则进行处理
        if (StringUtils.isNotBlank(exportFields)) {
            // 将逗号分隔的字段转为数组，调用showColumn指定显示字段
            String[] fields = exportFields.split(",");
            util.showColumn(fields); // 关键：使用ExcelUtil已有的showColumn方法
        }

        util.exportExcel(response, list, "一流学科数据");
    }

    /**
     * 获取一流学科详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(firstClassService.selectFirstClassById(id));
    }

    /**
     * 新增一流学科
     */
    @Log(title = "一流学科", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FirstClass firstClass)
    {
        FirstClassMember firstClassMember = new FirstClassMember();
        if(firstClass.getId()!=null){
            firstClassMember.setClassId(firstClass.getId());
        }else {
            firstClassMember.setClassId(-1L);
        }
        List<FirstClassMember> firstClassMembers = firstClassMemberService.selectFirstClassMemberList(firstClassMember);
        Long userId = firstClass.getTeacherId() ;
        if(firstClassMembers!=null &&! firstClassMembers.isEmpty()){
           for(FirstClassMember firstClassMember1:firstClassMembers){
               if(firstClassMember1.getMemberId().equals(userId)){
                   return error("此项目您已经参与并有名次记录，请勿重复添加");
               }
           }
        }
        return toAjax(firstClassService.insertFirstClass(firstClass));
    }

    /**
     * 修改一流学科
     */
    @Log(title = "一流学科", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FirstClass firstClass)
    {
        return toAjax(firstClassService.updateFirstClass(firstClass));
    }

    /**
     * 删除一流学科
     */
    @Log(title = "一流学科", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(firstClassService.deleteFirstClassByIds(ids));
    }

    /**
     * 根据关键字查询
     */
    @GetMapping("/getInfoByTitle")
    public AjaxResult getInfoByTitle(  String data){
        FirstClass ra = firstClassService.getInfoByTitle(data);
        return success(ra);
    }
}
